package cn.gx.kevin.common.utils;

import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.crypto.digests.SM3Digest;

import java.nio.charset.StandardCharsets;

public class SM3Utils {

    public static String sm3Hex(byte[] data) {
        return Hex.encodeHexString(sm3(data));
    }

    public static String sm3Hex(String data) {
        return Hex.encodeHexString(sm3(data.getBytes(StandardCharsets.UTF_8)));
    }

    public static byte[] sm3(byte[] data) {
        SM3Digest d = new SM3Digest();
        d.update(data, 0, data.length);
        byte[] hash = new byte[d.getDigestSize()];
        d.doFinal(hash, 0);
        return hash;
    }
}
